package com.witmed.mymdm.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.example.quickadapter.BaseQuickMultipleAdapter
import com.witmed.lib_base.BaseFragment
import com.witmed.mymdm.databinding.FragmentHomeBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewModel>() {

    val homeAdapter:HomeAdapter by lazy { HomeAdapter() }
    override fun createVM(): HomeViewModel {
        return ViewModelProvider(this).get(HomeViewModel::class.java)
    }

    override fun createV(): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(layoutInflater)
    }

    override fun bindState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                vm.mutableContainer.uiFlow.collect{
                    homeAdapter.addAll(it.list)
                }
            }
        }
    }

    override fun initData() {
        vm.createData()
    }

    override fun initView() {
        v.rv.layoutManager=GridLayoutManager(context,3)
        v.rv.adapter=homeAdapter
        homeAdapter.onItemClickListener=object :BaseQuickMultipleAdapter.OnItemClickListener{
            override fun onItemClick(position: Int) {
                when(position){
                    1->ARouter.getInstance().build("/apps/NetAppActivity").navigation()
                    2->ARouter.getInstance().build("/apps/LocalActivity").navigation()
                    7->ARouter.getInstance().build("/doc/DocumentActivity").navigation()
                }

            }
        }
        homeAdapter.onItemLongClickListener=object :BaseQuickMultipleAdapter.OnItemLongClickListener{
            override fun onItemLongClick(position: Int):Boolean {
                Toast.makeText(context, ""+homeAdapter.data.get(position).title, Toast.LENGTH_SHORT).show()
                return true
            }

        }
    }

    override fun onDestroyView() {
        super.onDestroyView()

    }
}